(function(){
	$.extend($.fn.datagrid.methods, {
		exportCsv:function(jq) {
			var datagrid = $.data(jq[0], "datagrid")
			var options = datagrid.options;
			if (!options) {
				console.log('获取配置信息失败');
				return;
			}
			var data = datagrid.data;
			var columns = options.columns;
	
			var queryParams = options.queryParams;
			queryParams.pageSize = data.total;
	
			var target = '';
			if (columns[0]) {
				for (var i = 0; i < columns[0].length; i++) {
					target += columns[0][i].title + ",";
				}
			}
			if (new RegExp(",$").test(target)) {
				target = target.substring(0, target.length - 1);
			}
			target += '\n';
			var formValues = $(options.toolbar).find('form').serialize();
			formValues = formValues.replace(/\+/g, " "); // g表示对整个字符串中符合条件的都进行替换  
			var url = options.url;
	
			var param = JSON.parse(decodeURIComponent(JSON.stringify(conveterParamsToJson(formValues))));
			for ( var v in param) {
				queryParams[v] = param[v];
			}
	
			$.ajax({
				"type" : "post",
				"dataType" : "json",
				"data" : queryParams,
				"contentType" : "application/x-www-form-urlencoded; charset=UTF-8",
				"url" : url,
				"success" : function(result) {
					for (var i = 0; i < result.rows.length; i++) {
						for (var j = 0; j < columns[0].length; j++) {
							var column = columns[0][j];
							var value = result.rows[i][column.field];
							if (column.formatter) {
								value = column.formatter(value, result.rows[i], j);
							}
							value = value.replace(/<[^>]+>/g, "");
							target += value + ",";
						}
						if (new RegExp(",$").test(target)) {
							target = target.substring(0, target.length - 1);
						}
						target += '\n';
					}
	
					target = "\ufeff" + target;
					var blob = new Blob([ target ], {
						type : 'text/csv'
					}); 
					var fileName = new Date().Format('yyyyMMddhhmmss') + ".csv";
	
					if (window.navigator.msSaveOrOpenBlob) {
						navigator.msSaveBlob(blob, fileName);
					} else {
						var link = document.createElement('a');
						link.href = window.URL.createObjectURL(blob);
						link.download = fileName;
						link.click();
						window.URL.revokeObjectURL(link.href);
					}
	
				}
			});
		}
	});
})(jQuery)